<?php



class Tests extends CI_Controller{

	

	public $db;



	private function setup($fixture){

		$this->db = $this->load->database('local_test', true);

		$this->load->dbforge();

		$this->load->library('unit_test');

		require BASE_PATH.'application/fixtures/'.$fixture.'.php';

		foreach($fixture_tables as $name=>$fields){

			$this->dbforge->drop_table($name);

			$this->dbforge->add_field('id');

			$this->dbforge->add_field($fields);

			$this->dbforge->create_table($name);

		}

		foreach($fixture_data as $table=>$data){

			foreach($data as $row){

				$this->db->insert($table, $row);

			}

		}

	}

	

	

	public function user_tests(){

		$this->setup('users');

		$this->load->model('User_model', 'user');

		$valid_user = array(

			'email' => 'paul.wilson66@gmail.com',

			'password' => 'password',
			'password_confirm' => 'password',
			'user_type_id' => 2,
			'first_name' => 'Paul',
			'last_name' => 'Wilson'

		);

		$user_no_password = array(

			'email' => 'paul.wilson67@gmail.com',
			'password' => '',
			'password_confirm' => 'password',
			'first_name' => 'Paul',
			'last_name' => 'Wilson'

		);

		$user_no_email = array(

			'email' => '',
			'password' => 'password',
			'password_confirm' => 'password',
			'first_name' => 'Paul',
			'last_name' => 'Wilson'

		);

		$user_invalid_email = array(

			'email' => 'paul.wilson.com',
			'password' => 'password',
			'password_confirm' => 'password',
			'first_name' => 'Paul',
			'last_name' => 'Wilson'

		);

		$test1 = $this->user->add($valid_user);
		
		$test1_1 = $this->user->add($valid_user);

		$test2 = $this->user->add($user_no_password);

		$test3 = $this->user->add($user_no_email);

		$test4 = $this->user->add($user_invalid_email);

		$test5 = $this->user->find_by_email('paul.wilson66@gmail.com');

		$test6 = $this->user->find_by_email('notexisting@gmail.com');

		

		$this->user->change_email($test5['id'], 'paul.wilson88@gmail.com');

		$r = $this->user->find_by_id($test5['id']);

		$test7 = $r['email'];

		

		$token = $this->user->reset_password_request($r['email']);

		$user = $this->user->find_by_email($r['email']);

		$test8 = $user['password_reset_token'];

		

		$invalid_token = 'c64eaf621d95379515b97afe40613ccc';

		$test9 = $this->user->reset_password($r['email'], $invalid_token, 'new_password');

		$test10 = $this->user->reset_password($r['email'], $token, 'new_password');

		

		$test11 = $this->user->delete($test5['id']);

		$this->unit->run($test1, 'is_true', 'Add Valid User');

		$this->unit->run($test1_1, 'is_false', 'Exisiting User');

		$this->unit->run($test2, 'is_false', 'Adding user without a password');

		$this->unit->run($test3, 'is_false', 'Adding user without email');

		$this->unit->run($test4, 'is_false', 'Adding a user with an invalid email');

		$this->unit->run($test5, 'is_array', 'Searching for exisiting user');

		$this->unit->run($test6, 'is_false', 'Searching for a non-existant user');

		$this->unit->run($test7, 'paul.wilson88@gmail.com', 'Changing users email address', $test7); 

		$this->unit->run($test8, $token, 'Generating Password reset token', "Returned: {$token}, in DB: {$test8}");

		$this->unit->run($test9, 'is_false', 'Attempting to update password with invalid token');

		$this->unit->run($test10, 'is_true', "Valid password reset call");

		$this->unit->run($test11, 'is_false', 'Trying to delete last user');

		echo $this->unit->report();

	}

	

	public function login_tests(){

		$this->setup('users');

		$this->load->model('User_model', 'user');

		$this->load->model('Login_model', 'login');

		$valid_user = array(

			'email' => 'paul.wilson66@gmail.com',

			'password' => 'password',

			'user_type_id' => 2

		);

		$this->user->add($valid_user);

		$test1 = $this->login->login('paulwilson66@gmail.com', 'password');

		$test2 = $this->login->login('paul.wilson66@gmail.com', '');

		$test3 = $this->login->login('paul.wilson66@gmail.com', 'password');

		$test4 = $this->login->is_logged_in();

		$test5 = $this->login->is_admin();

		$test6 = $this->login->current_user();

		

		$this->unit->run($test1, 'is_false', 'log-in attempt with wrong email' );

		$this->unit->run($test2, 'is_false', 'log-in attempt with wrong password');

		$this->unit->run($test3, 'is_true', 'log-in attempt with correct info');

		$this->unit->run($test4, 'is_true', 'testing is user is logged in');

		$this->unit->run($test5, 'is_true', 'testing if user is admin');

		$this->unit->run($test6, 'is_array', 'get current user' );

		echo $this->unit->report();

	}

	



	

	

	

}